package com.example

/**
 * 使用集合
 * Created by Walkud on 17/8/21.
 */
object UseList {

    data class Customer(val name: String, val email: String)

    @JvmStatic
    fun main(array: Array<String>) {
        useFor()
        println()
        inWhen()
        println()
        useLambda()
        println()
        studyMap()
        println()

    }

    /**
     * 集合迭代List , Map
     */
    fun useFor() {
        val items = getItems()
        for (item in items) {
            println("item:$item")
        }

        val mapItems = getMapItems()

        for ((key, value) in mapItems) {
            println("${key}价格： $value")
        }

    }

    /**
     * 使用in运算符判断集合是否包含示例
     */
    fun inWhen() {

        val items = getItems()

        when {
        //只会执行其一
            "Orange" in items -> println("包含Orange")
            "Apple" in items -> println("包含Apple")
        }
    }

    /**
     * 使用Lambda
     */
    fun useLambda() {
        val items = getItems()

        items.filter { it.startsWith("A") }//首字母包含A
                .sortedBy { it }//排序
                .map { it.toUpperCase() }//转换大写
                .forEach { println(it) }//循环输出

    }

    fun studyMap() {
        val mapItems = getMapItems()

        println(mapItems["Pear"])
//        mapItems["Pear"].to("￥10")


        val mutableMaps = getMutableMap()
        println(mutableMaps["Pear"])
        mutableMaps["Pear"] = "bb"
        println(mutableMaps["Pear"])
    }


    /**
     * 不可变List
     */
    fun getItems(): List<String> {
        return listOf("Apple", "Orange", "Pear", "Abc")
    }

    /**
     * 可变List
     */
    fun getMutableItems(): MutableList<String> {
        return mutableListOf("Apple", "Orange", "Pear", "Abc")
    }

    /**
     * 可变Map
     */
    fun getMutableMap(): MutableMap<String, String> {
        return mutableMapOf("Apple" to "￥7", "Orange" to "￥4", "Pear" to "￥5", "Abc" to "Null");
    }

    /**
     * 不可变Map
     */
    fun getMapItems(): Map<String, String> {
        return mapOf("Apple" to "￥7", "Orange" to "￥4", "Pear" to "￥5", "Abc" to "Null")
    }
}